package com.example.fitapp.activity.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.fitapp.activity.R;
import com.example.fitapp.activity.adapter.HomeDataAdapter;
import com.example.fitapp.activity.bean.DynamicInfo;
import com.example.fitapp.activity.http.api.dynamic.GetDynamicAll;
import com.example.fitapp.activity.http.util.ApiListener;
import com.example.fitapp.activity.http.util.ApiUtil;
import com.example.fitapp.activity.utils.L;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends Fragment {
    private List<DynamicInfo.DataBean> dataBeans = new ArrayList<>();
    private static final String BUNDLE_KEY_TITLE = "key_title";
    private HomeDataAdapter homeDataAdapter;
    private RecyclerView rv_home;
    private StaggeredGridLayoutManager linearLayoutManager;

    public static HomeFragment newInstance(String title){
        Bundle bundle = new Bundle();
        bundle.putString(BUNDLE_KEY_TITLE,title);
        HomeFragment homeFragment = new HomeFragment();
        homeFragment.setArguments(bundle);
        return homeFragment;
    }
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.home_fragment_layou,null);

        linearLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        linearLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);

        rv_home = view.findViewById(R.id.rv_home);
        ((DefaultItemAnimator) rv_home.getItemAnimator()).setSupportsChangeAnimations(false);
        ((SimpleItemAnimator) rv_home.getItemAnimator()).setSupportsChangeAnimations(false);
        rv_home.getItemAnimator().setAddDuration(0);
        rv_home.setLayoutManager(linearLayoutManager);
        onListenerScroll();



        homeDataAdapter = new HomeDataAdapter(dataBeans); // 渲染的json数据体
        homeDataAdapter.setContext(getContext());
        rv_home.setAdapter(homeDataAdapter);
        getHomeData();

        // 悬浮按钮这里的代码可以不要
        /*
        FloatingActionButton fab = (FloatingActionButton) view.findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HomeFragment.this.getContext(),PushActivity.class);
                startActivity(intent);
            }
        });
         */

        return view;
    }

    // 监听recelyView的滑动事件
    private void  onListenerScroll( ){
        rv_home.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                // 防止第一行有空白
                linearLayoutManager.invalidateSpanAssignments();
            }
        });
    }


    private void getHomeData() {
        new GetDynamicAll().get(new ApiListener(){
            @Override
            public void success(ApiUtil apiUtil) {
                GetDynamicAll api = (GetDynamicAll)apiUtil;
                String response = api.mResponse;
                    DynamicInfo jsonben =  new Gson().fromJson(response,DynamicInfo.class);
                    dataBeans.addAll(jsonben.getData());  // 把请求到的数据里面的Data数据体传到dataBeans对象里面
                    homeDataAdapter.initImageScale();
                    homeDataAdapter.notifyDataSetChanged();

            }

            @Override
            public void failure(ApiUtil apiUtil) {

            }
        });
//        旧的获取请求方法，到时候移除
        /*
        HttpUtil.get("/v1/dynamic/getAll", new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData = response.body().string();
                Log.d("MainActivity","Successs"+responseData);

                DynamicInfo jsonben =  new Gson().fromJson(responseData,DynamicInfo.class);

                dataBeans.addAll(jsonben.getData());  // 把请求到的数据里面的Data数据体传到dataBeans对象里面

                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        homeDataAdapter.initImageScale();
                        homeDataAdapter.notifyDataSetChanged();
                    }
                });

            }
        });
        */
    }
}
